package com.excuse.excusebuilder.presenter;

import android.content.Context;

import com.excuse.excusebuilder.bean.MessageLeft;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.bean.User;
import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.commen.util.ToastUtil;
import com.excuse.excusebuilder.impl.callback.GetLeftMsgsCallback;
import com.excuse.excusebuilder.impl.model.CommunityItemDetailModelInterface;
import com.excuse.excusebuilder.impl.view.CommunityItemDetailInterface;
import com.excuse.excusebuilder.model.CommunityItemDetailModel;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by U-nookia on 2017/4/20.
 */

public class CommunityItemDetailPresenter implements GetLeftMsgsCallback{
    private Context context;
    private CommunityItemDetailInterface communityItemDetail;
    private CommunityItemDetailModelInterface communityItemDetailModel;

    public CommunityItemDetailPresenter(Context context, CommunityItemDetailInterface communityItemDetail) {
        this.context = context;
        this.communityItemDetail = communityItemDetail;
        communityItemDetailModel = new CommunityItemDetailModel();
    }

    public void initLeftMsgs(Post post) {
        communityItemDetailModel.getLeftMsgsFromBmob(post,this);
    }

    @Override
    public void setList(List<MessageLeft> msgs) {
        communityItemDetail.setAdapter(msgs);
    }

    @Override
    public void setThumbsUpImg() {
        communityItemDetail.setThumbsUpImg();
    }

    @Override
    public void cancelThumbsUpImg() {
        communityItemDetail.cancelThumbsUpImg();
    }

    @Override
    public void haveSaveMsg(MessageLeft msg) {
        communityItemDetail.haveSaveMsg(msg);
    }

    @Override
    public void haveError(String string) {
        ToastUtil.makeShortToast(context,string);
    }

    public void queryThumbsUp(Post post) {
        communityItemDetailModel.queryThumbsUpOnBmob(post,this);
    }

    public void addThumbsUpToBmob(final Post post) {
        User user = BmobUser.getCurrentUser(User.class);
        if (user==null){
            ToastUtil.makeShortToast(context,"请先登录");
            return;
        }
        if (!NetWorkUtil.netWorkConnected){
            ToastUtil.makeShortToast(context,"请检查网络");
            return;
        }
        BmobRelation relation = new BmobRelation();
        relation.add(user);
        post.setRelation(relation);
        post.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e != null) {
                    ToastUtil.makeShortToast(context, "点赞出错，请重试");
                    cancelThumbsUpImg();
                    return;
                }
                post.increment("likeNum",1);
                post.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e != null) {
                            ToastUtil.makeShortToast(context, "点赞出错，请重试");
                            cancelThumbsUpImg();
                        }
                    }
                });
            }
        });
    }

    public void deleteThumbsUpOnBmob(final Post post) {
        User user = BmobUser.getCurrentUser(User.class);
        if (user==null){
            ToastUtil.makeShortToast(context,"请先登录");
            return;
        }
        if (!NetWorkUtil.netWorkConnected){
            ToastUtil.makeShortToast(context,"请检查网络");
            return;
        }
        BmobRelation relation = new BmobRelation();
        relation.remove(user);
        post.setRelation(relation);
        post.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e!=null){
                    ToastUtil.makeShortToast(context, "取消点赞出错，请重试");
                    setThumbsUpImg();
                    return;
                }
                post.increment("likeNum",-1);
                post.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e != null) {
                            ToastUtil.makeShortToast(context, "取消点赞出错，请重试");
                            cancelThumbsUpImg();
                        }
                    }
                });
            }
        });
    }

    public void saveLeftMsgToBmob(Post post, String string) {
        if (string.equals("")) {
            ToastUtil.makeShortToast(context,"输入不能为空");
            return;
        }
        if (BmobUser.getCurrentUser()==null){
            ToastUtil.makeShortToast(context,"请先登录");
            return;
        }
        if (!NetWorkUtil.netWorkConnected){
            ToastUtil.makeShortToast(context,"请检查网络");
            return;
        }
        communityItemDetailModel.saveLeftMsgToBmob(post,string,this);
    }
}
